package com.example.fingerdemo;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Handler;
import android.os.Looper;

public class ConSample extends Thread {
    private String TAG = getClass().getSimpleName();
    private Activity mContext;
    public boolean runing = false;
    short[] image_con = new short[(256 * 360 + 1078) / 2];
    byte[] bmp_con = new byte[256 * 360 + 1078];
    public String filename = null;
    public boolean inited = false;


    @SuppressLint("HandlerLeak")
    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(android.os.Message msg) {
            if (msg.what == 3) {
                mIDCardRecListener.onResp((String) msg.obj);
            } else if (msg.what == 1) {
                mIDCardRecListener.onRespBytes((byte[]) msg.obj);
            } else {
                mIDCardRecListener.onResp(null);
            }
        }

        ;
    };

    public ConSample(Activity context, SampleListener listener) {
        mContext = context;
        //runing = true;
        mIDCardRecListener = listener;
    }

    public void close() {
        runing = false;
    }

    @Override
    public void run() {
        int ret = 1;
        //filename = Environment.getExternalStorageDirectory().getAbsolutePath()+"/finger_con.bmp";
        androidbmp mandroidbmp = null;
        mandroidbmp = new androidbmp();
        super.run();
        Looper.prepare();
        while (true) {

            if (this.runing && this.inited) {
                mHandler.sendEmptyMessage(2);

                ret = finger.LIVESCAN_GetFPBmpData(0, image_con);

                //ret = finger.LIVESCAN_GetFPRawData(0, sampleimage);
                if (ret != 1) {
                    this.inited = false;
                    mHandler.sendMessage(mHandler.obtainMessage(3, "dataerror"));
                } else {
                    for (int i = 0; i < (256 * 360 + 1078) / 2; i++) {
                        bmp_con[2 * i + 1] = (byte) (image_con[i] >> 8 & 0xff);
                        bmp_con[2 * i] = (byte) (image_con[i] & 0xff);
                    }
                    int image_length = bmp_con.length;
//                    File mFile = new File(filename);
//                    if (mFile.exists()) {
//                        mFile.delete();
//                    }
//                    try {
//                        mandroidbmp.save_bmp(filename,bmp_con);
//
//                    } catch (IOException e) {
//                        e.printStackTrace();
//                    }

                    mHandler.sendMessage(mHandler.obtainMessage(1, bmp_con));
                }

            } else {
                mHandler.sendEmptyMessage(2);
            }
            if (!this.inited && ret == 0) {
                mHandler.sendMessage(mHandler.obtainMessage(3, "dataerror"));
            }
            sleepTime(1000);

        }
    }

    private void sleepTime(long time) {
        try {
            sleep(time);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    }

    private SampleListener mIDCardRecListener;

    public interface SampleListener {
        public void onResp(String info);

        public void onRespBytes(byte[] info);
    }


}
